package com.duoec.dw.lineage.service.impl.analyser.statement;

import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr;
import com.alibaba.druid.sql.ast.statement.SQLAlterTableItem;
import com.alibaba.druid.sql.ast.statement.SQLAlterTableStatement;
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
import com.duoec.dw.lineage.dto.DdlChangeDto;
import com.duoec.dw.lineage.dto.DdlChangeItemDto;
import com.duoec.dw.lineage.core.utils.AnalyserUtils;
import com.duoec.dw.lineage.service.AlterTableItemAnalyser;
import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author xuwenzhen
 */
@Service
public class MySqlAlterTableStatementAnalyser extends BaseStatementAnalyser<SQLAlterTableStatement, DdlChangeDto> {
    private static final Logger logger = LoggerFactory.getLogger(MySqlAlterTableStatementAnalyser.class);

    /**
     * 获取当前分析器分析的statement类型
     *
     * @return statement类型
     */
    @Override
    public Class<SQLAlterTableStatement> getType() {
        return SQLAlterTableStatement.class;
    }

    /**
     * 解析语句
     *
     * @param statement SQL语句
     * @return 返回值
     */
    @Override
    public DdlChangeDto analyse(SQLAlterTableStatement statement) {
        List<SQLAlterTableItem> items = statement.getItems();
        if (CollectionUtils.isEmpty(items)) {
            return null;
        }

        DdlChangeDto dto = new DdlChangeDto();
        SQLExprTableSource tableSource = statement.getTableSource();
        String tableName = null;
        String databaseName = null;
        if (tableSource != null && tableSource.getExpr() != null) {
            SQLExpr expr = tableSource.getExpr();
            if (expr instanceof SQLPropertyExpr) {
                SQLPropertyExpr propertyExpr = (SQLPropertyExpr) expr;
                tableName = propertyExpr.getName();
                databaseName = propertyExpr.getOwnernName();
            } else if (expr instanceof SQLIdentifierExpr) {
                tableName = expr.toString();
            }
        }

        if (databaseName != null) {
            dto.setDatabaseName(AnalyserUtils.formatFieldName(databaseName));
        }
        if (tableName != null) {
            dto.setTableName(AnalyserUtils.formatFieldName(tableName));
        }

        List<DdlChangeItemDto> changeList = Lists.newArrayList();
        dto.setItems(changeList);
        items.forEach(item -> {
            AlterTableItemAnalyser analyser = mySqlAnalyserConfig.getAlterTableItemAnalyser(item);
            if (analyser == null) {
                logger.error("未实现类[{}]的解析器！", item.getClass().getName());
//                throw new LineageAnalyseException("未实现类[" + item.getClass().getName() + "]的解析器！");
                return;
            }
            List<DdlChangeItemDto> ddlChangeList = analyser.analyse(item);
            if (!CollectionUtils.isEmpty(ddlChangeList)) {
                changeList.addAll(ddlChangeList);
            }
        });
        return dto;
    }
}
